# 临时保存用户身份证验证信息, 需要定期清除,避免保存敏感数据

config = require('../config')
mongoose = config.mongoose
updated_at = config.mongoose_plugins.updated_at
Schema = config.Schema

Q = require("q")

TmpUserSchema = new Schema(
  portal_id: String   # 广发通id
  real_name: String   # 姓名
  id_no:     String   # 身份证明
  bank_card_no: String # 银行卡
  created_at:
    type: Date
    default: new Date()
)


TmpUserSchema.index {portal_id:1}, {unique:true}

exports.TmpUser = TmpUser = mongoose.model "TmpUser", TmpUserSchema

exports.intern_tmp_user = (portal_id,real_name,id_no,bank_card_no) ->
  find_tmp_user(portal_id)
    .then (user) ->
      if user
        user.real_name = real_name
        user.id_no = id_no
        user.bank_card_no = bank_card_no
        return Q.ninvoke(user,"save")
      else
        user = new TmpUser {portal_id,real_name,id_no,bank_card_no}
        return Q.ninvoke(user,"save")

exports.find_tmp_user = find_tmp_user = (portal_id) ->
  Q.ninvoke(TmpUser,"findOne",{portal_id})



TmpUserInfoSchema = new Schema(
  uid: String
  info: Schema.Types.Mixed
  created_at:
    type: Date
    default: new Date()
)

exports.TmpUserInfo = TmpUserInfo = mongoose.model "TmpUserInfo", TmpUserInfoSchema

exports.create_tmp_user_info = (uid,info) ->

  info = new TmpUserInfo {uid,info}
  Q.ninvoke(info, "save")
    .then ->
      return info

 
exports.find_tmp_user_info = (id) ->
  Q.ninvoke(TmpUserInfo,"findOne",{_id:id})
 
